package com.zu.commu.context;

import com.zu.commu.dataSource.conf.DbConfigVO;
import com.zu.commu.exception.CommuException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import java.io.File;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

public class ParseServerConf {
	private Document doc;
	public void createDom() throws CommuException {
		URL configFileURL = ParseServerConf.class.getResource("/Server.xml");
		if (configFileURL == null) {
			configFileURL = ParseServerConf.class.getResource("/server.xml");
		}
		if (configFileURL == null)
			throw new CommuException().setMessage("没有得到Server.xml配置!", null);
		try {
			SAXBuilder sb = new SAXBuilder();
			this.doc = sb.build(configFileURL);
			if (this.doc == null)
				throw new CommuException().setMessage(
						"没有生成Server.xml配置的DOM对象!", null);
		} catch (Exception e) {
			throw new CommuException().setMessage("没有得到Server.xml配置!", e);
		}
	}

	public void parseServerOptions() throws CommuException {
		Element root = this.doc.getRootElement();
		if (root == null) {
			throw new CommuException().setMessage(
					"没有得到Server.xml配置根元素configs!", null);
		}
		Element options = root.getChild("options");
		if (options == null) {
			throw new CommuException().setMessage("Server.xml中元素options配置不正确!",
					null);
		}

		Element temp = options.getChild("remotePort");
		String ct = temp.getText();
		if ((temp == null) || (ct == null) || (ct.trim().equals("")))
			throw new CommuException().setMessage(
					"Server.xml中元素remotePort配置不正确!", null);
		try {
			ServerContext.remotePort = Integer.parseInt(ct.trim());
		} catch (Exception eint) {
			throw new CommuException().setMessage(
					"Server.xml中元素remotePort配置不正确!", eint);
		}

		temp = options.getChild("localPort");
		ct = temp.getText();
		if ((temp == null) || (ct == null) || (ct.trim().equals("")))
			throw new CommuException().setMessage(
					"Server.xml中元素localPort配置不正确!", null);
		try {
			ServerContext.localPort = Integer.parseInt(ct.trim());
		} catch (Exception eint) {
			throw new CommuException().setMessage(
					"Server.xml中元素localPort配置不正确!", eint);
		}

		temp = options.getChild("bufferSizeForReadRemoteSocket");
		ct = temp.getText();
		if ((temp == null) || (ct == null) || (ct.trim().equals("")))
			throw new CommuException().setMessage(
					"Server.xml中元素bufferSizeForReadRemoteSocket配置不正确!", null);
		try {
			ServerContext.bufferSizeForReadRemoteSocket = Integer.parseInt(ct
					.trim());
		} catch (Exception eint) {
			throw new CommuException().setMessage(
					"Server.xml中元素bufferSizeForReadRemoteSocket配置不正确!", eint);
		}

		temp = options.getChild("bufferSizeForReadLocalSocket");
		ct = temp.getText();
		if ((temp == null) || (ct == null) || (ct.trim().equals("")))
			throw new CommuException().setMessage(
					"Server.xml中元素bufferSizeForReadLocalSocket配置不正确!", null);
		try {
			ServerContext.bufferSizeForReadLocalSocket = Integer.parseInt(ct
					.trim());
		} catch (Exception eint) {
			throw new CommuException().setMessage(
					"Server.xml中元素bufferSizeForReadLocalSocket配置不正确!", eint);
		}

		temp = options.getChild("minThreadNumRemote");
		ct = temp.getText();
		if ((temp == null) || (ct == null) || (ct.trim().equals("")))
			throw new CommuException().setMessage(
					"Server.xml中元素minThreadNumRemote配置不正确!", null);
		try {
			ServerContext.minThreadNumRemote = Integer.parseInt(ct.trim());
		} catch (Exception eint) {
			throw new CommuException().setMessage(
					"Server.xml中元素minThreadNumRemote配置不正确!", eint);
		}

		temp = options.getChild("maxThreadNumRemote");
		ct = temp.getText();
		if ((temp == null) || (ct == null) || (ct.trim().equals("")))
			throw new CommuException().setMessage(
					"Server.xml中元素maxThreadNumRemote配置不正确!", null);
		try {
			ServerContext.maxThreadNumRemote = Integer.parseInt(ct.trim());
		} catch (Exception eint) {
			throw new CommuException().setMessage(
					"Server.xml中元素maxThreadNumRemote配置不正确!", eint);
		}

		temp = options.getChild("minThreadNumLocal");
		ct = temp.getText();
		if ((temp == null) || (ct == null) || (ct.trim().equals("")))
			throw new CommuException().setMessage(
					"Server.xml中元素minThreadNumLocal配置不正确!", null);
		try {
			ServerContext.minThreadNumLocal = Integer.parseInt(ct.trim());
		} catch (Exception eint) {
			throw new CommuException().setMessage(
					"Server.xml中元素minThreadNumLocal配置不正确!", eint);
		}

		temp = options.getChild("maxThreadNumLocal");
		ct = temp.getText();
		if ((temp == null) || (ct == null) || (ct.trim().equals("")))
			throw new CommuException().setMessage(
					"Server.xml中元素maxThreadNumLocal配置不正确!", null);
		try {
			ServerContext.maxThreadNumLocal = Integer.parseInt(ct);
		} catch (Exception eint) {
			throw new CommuException().setMessage(
					"Server.xml中元素maxThreadNumLocal配置不正确!", eint);
		}

		temp = options.getChild("cleanSocketCommandIdThreadSleepTime");
		ct = temp.getText();
		if ((temp == null) || (ct == null) || (ct.trim().equals("")))
			throw new CommuException().setMessage(
					"Server.xml中元素cleanSocketCommandIdThreadSleepTime配置不正确!",
					null);
		try {
			ServerContext.cleanSocketCommandIdThreadSleepTime = Integer
					.parseInt(ct.trim());
		} catch (Exception eint) {
			throw new CommuException().setMessage(
					"Server.xml中元素cleanSocketCommandIdThreadSleepTime配置不正确!",
					eint);
		}

		temp = options.getChild("commandIdExistTime");
		ct = temp.getText();
		if ((temp == null) || (ct == null) || (ct.trim().equals("")))
			throw new CommuException().setMessage(
					"Server.xml中元素commandIdExistTime配置不正确!", null);
		try {
			ServerContext.commandIdExistTime = Integer.parseInt(ct.trim());
		} catch (Exception eint) {
			throw new CommuException().setMessage(
					"Server.xml中元素commandIdExistTime配置不正确!", eint);
		}

		temp = options.getChild("flagSocketOutLineThreadSleepTime");
		ct = temp.getText();
		if ((temp == null) || (ct == null) || (ct.trim().equals("")))
			throw new CommuException()
					.setMessage(
							"Server.xml中元素flagSocketOutLineThreadSleepTime配置不正确!",
							null);
		try {
			ServerContext.flagSocketOutLineThreadSleepTime = Integer
					.parseInt(ct.trim());
		} catch (Exception eint) {
			throw new CommuException()
					.setMessage(
							"Server.xml中元素flagSocketOutLineThreadSleepTime配置不正确!",
							eint);
		}

		temp = options.getChild("noHearBeatTimes");
		ct = temp.getText();
		if ((temp == null) || (ct == null) || (ct.trim().equals("")))
			throw new CommuException().setMessage(
					"Server.xml中元素noHearBeatTimes配置不正确!", null);
		try {
			ServerContext.noHearBeatTimes = Integer.parseInt(ct.trim());
		} catch (Exception eint) {
			throw new CommuException().setMessage(
					"Server.xml中元素noHearBeatTimes配置不正确!", eint);
		}

		temp = options.getChild("useLocalDatabase");
		ct = temp.getText();
		if ((temp == null) || (ct == null) || (ct.trim().equals("")))
			throw new CommuException().setMessage(
					"Server.xml中元素useLocalDatabase配置不正确!", null);
		try {
			if (ct.trim().equals("true")) {
				ServerContext.useLocalDatabase = true;
			} else if (ct.trim().equals("false"))
				ServerContext.useLocalDatabase = false;
			else
				throw new CommuException().setMessage(
						"Server.xml中元素useLocalDatabase配置不正确,必须是true或false!",
						null);
		} catch (Exception eint) {
			throw new CommuException().setMessage(
					"Server.xml中元素useLocalDatabase配置不正确!", eint);
		}
	}

	public void parseGSMOptions() throws CommuException {
		Element root = this.doc.getRootElement();
		if (root == null) {
			throw new CommuException().setMessage(
					"没有得到Server.xml配置根元素configs!", null);
		}
		Element gsmOption = root.getChild("gsmOption");
		if (gsmOption == null) {
			throw new CommuException().setMessage(
					"Server.xml中元素gsmOption配置不正确!", null);
		}

		if (!"true".equalsIgnoreCase(gsmOption.getAttributeValue("enabled"))) {
			System.out.println("--配置GSM通讯服务不启动!");
			ServerContext.GSMServerEnabled = Boolean.valueOf(false);
			ServerContext.GSMServerDevice = null;
			ServerContext.GSMServerBaud = null;
			ServerContext.GSMServerSN = null;
			ServerContext.GSMDebug = null;
			ServerContext.GSMDebugSimNO = null;
			ServerContext.DebugSavePath = null;
			return;
		}
		ServerContext.GSMServerEnabled = Boolean.valueOf(true);
		Element temp = gsmOption.getChild("device");
		String ct = temp.getText();
		if ((temp == null) || (ct == null) || (ct.trim().equals(""))) {
			throw new CommuException().setMessage(
					"Server.xml中元素gsmOption.device配置不正确!", null);
		}
		ServerContext.GSMServerDevice = ct;

		temp = gsmOption.getChild("baud");
		ct = temp.getText();
		if ((temp == null) || (ct == null) || (ct.trim().equals("")))
			throw new CommuException().setMessage(
					"Server.xml中元素gsmOption.baud配置不正确!", null);
		try {
			ServerContext.GSMServerBaud = Integer.valueOf(Integer.parseInt(ct
					.trim()));
		} catch (Exception eint) {
			throw new CommuException().setMessage(
					"Server.xml中元素gsmOption.baud配置不正确!", eint);
		}

		temp = gsmOption.getChild("sn");
		ct = temp.getText();
		if ((temp == null) || (ct == null) || (ct.trim().equals(""))) {
			throw new CommuException().setMessage(
					"Server.xml中元素gsmOption.sn配置不正确!", null);
		}
		ServerContext.GSMServerSN = ct;

		temp = gsmOption.getChild("degug");
		if (temp == null) {
			throw new CommuException().setMessage(
					"Server.xml中元素gsmOption.degug配置不正确!", null);
		}

		ct = temp.getAttributeValue("enable");
		if ((ct == null)
				|| (ct.trim().equals(""))
				|| ((!ct.trim().equals("true")) && (!ct.trim().equals("false")))) {
			throw new CommuException().setMessage(
					"Server.xml中元素gsmOption.degug.enable配置不正确!", null);
		}
		if (ct.trim().equals("true"))
			ServerContext.GSMDebug = new Boolean(true);
		else {
			ServerContext.GSMDebug = new Boolean(false);
		}

		ct = temp.getAttributeValue("simNo");
		if (ct != null) {
			ServerContext.GSMDebugSimNO = ct.trim();
		}

		ct = temp.getText();
		if ((temp == null) || (ct == null) || (ct.trim().equals(""))) {
			throw new CommuException().setMessage(
					"Server.xml中元素gsmOption.degug配置不正确!", null);
		}
		ServerContext.DebugSavePath = ct;
	}

	public void parseToCenterCommunication() throws CommuException {
		Element root = this.doc.getRootElement();
		if (root == null) {
			throw new CommuException().setMessage(
					"没有得到Server.xml配置根元素configs!", null);
		}
		Element communication = root.getChild("communication");
		if (communication == null) {
			throw new CommuException().setMessage(
					"Server.xml中元素communication配置不正确!", null);
		}

		Element toCenter = communication.getChild("toCenter");
		if (toCenter == null) {
			throw new CommuException().setMessage(
					"Server.xml中元素communication.toCenter配置不正确!", null);
		}

		if (toCenter.getChildren().size() > 2) {
			throw new CommuException().setMessage(
					"Server.xml中元素communication.toCenter配置不正确,只能与中心有一种通信方式!",
					null);
		}

		Element temp = toCenter.getChild("type");
		String ct = temp.getText();
		if ((temp == null) || (ct == null) || (ct.trim().equals(""))) {
			throw new CommuException().setMessage(
					"Server.xml中元素communication.toCenter.type配置不正确!", null);
		}
		ServerContext.toCenterType = ct.trim();

		temp = toCenter.getChild("URL");
		ct = temp.getText();
		if ((temp == null) || (ct == null) || (ct.trim().equals(""))) {
			throw new CommuException().setMessage(
					"Server.xml中元素communication.toCenter.URL配置不正确!", null);
		}
		ServerContext.toCenterURL = ct.trim();
	}

	public void parseFromCenterCommunication() throws CommuException {
		Element root = this.doc.getRootElement();
		if (root == null) {
			throw new CommuException().setMessage(
					"没有得到Server.xml配置根元素configs!", null);
		}
		Element communication = root.getChild("communication");
		if (communication == null) {
			throw new CommuException().setMessage(
					"Server.xml中元素communication配置不正确!", null);
		}

		Element fromCenter = communication.getChild("fromCenter");
		if (fromCenter == null) {
			throw new CommuException().setMessage(
					"Server.xml中元素communication.fromCenter配置不正确!", null);
		}

		if (fromCenter.getChildren().size() > 2) {
			throw new CommuException().setMessage(
					"Server.xml中元素communication.fromCenter配置不正确,只能与中心有一种通信方式!",
					null);
		}

		Element temp = fromCenter.getChild("type");
		String ct = temp.getText();
		if ((temp == null) || (ct == null) || (ct.trim().equals(""))) {
			throw new CommuException().setMessage(
					"Server.xml中元素communication.fromCenter.type配置不正确!", null);
		}
		ServerContext.fromCenterType = ct.trim();
	}

	public void parseDbConfig() throws CommuException {
		DbConfigVO[] dbConfigVO = (DbConfigVO[]) null;
		Element root = this.doc.getRootElement();
		if (root == null) {
			throw new CommuException().setMessage("没有得到Server.xml配置根元素!", null);
		}
		Element dataSources = root.getChild("dataSources");
		if (dataSources == null) {
			throw new CommuException().setMessage(
					"Server.xml中元素dataSources配置不正确!", null);
		}

		List dataSourceList = dataSources.getChildren();
		if ((dataSourceList == null) || (dataSourceList.size() == 0)) {
			throw new CommuException().setMessage(
					"Server.xml中元素dataSource配置不正确!", null);
		}

		int num = dataSourceList.size();
		if (!ServerContext.useLocalDatabase) {
			num--;
		}

		dbConfigVO = new DbConfigVO[num];
		num = 0;

		Iterator it = dataSourceList.iterator();
		String temp = null;
		Element tempe = null;
		int tempInt = 0;
		while (it.hasNext()) {
			Element e = (Element) it.next();
			tempe = e.getChild("dataSourceName");
			if (tempe == null)
				throw new CommuException().setMessage(
						"Server.xml中元素dataSourceName配置不正确!", null);
			if ((!ServerContext.useLocalDatabase)
					&& (tempe.getText().equals("zuCommuServerLocalDS"))) {
				continue;
			}
			dbConfigVO[num] = new DbConfigVO();
			dbConfigVO[num].dataSourceName = tempe.getText();
			tempe = e.getChild("driverClass");
			if (tempe == null)
				throw new CommuException().setMessage(
						"Server.xml中元素driverClass配置不正确!", null);
			dbConfigVO[num].driverClass = tempe.getText();
			tempe = e.getChild("url");
			if (tempe == null)
				throw new CommuException().setMessage("Server.xml中元素url配置不正确!",
						null);
			dbConfigVO[num].url = tempe.getText();

			tempe = e.getChild("user");
			if (tempe == null)
				throw new CommuException().setMessage(
						"Server.xml中元素user配置不正确!", null);
			dbConfigVO[num].user = tempe.getText();

			tempe = e.getChild("password");
			if (tempe == null)
				throw new CommuException().setMessage(
						"Server.xml中元素password配置不正确!", null);
			dbConfigVO[num].password = tempe.getText();
			tempe = e.getChild("needTestConnection");
			if (tempe == null)
				throw new CommuException().setMessage(
						"Server.xml中元素needTestConnection配置不正确!", null);
			if ((!tempe.getText().equals("true"))
					&& (!tempe.getText().equals("false"))) {
				throw new CommuException().setMessage(
						"Server.xml中元素needTestConnection配置不正确!", null);
			}
			dbConfigVO[num].needTestConnection = (tempe.getText()
					.equals("true"));
			int timeConNeedTest = 0;
			try {
				timeConNeedTest = Integer.parseInt(e
						.getChild("timeConNeedTest").getText().trim());
			} catch (Exception eint) {
				throw new CommuException().setMessage(
						"Server.xml中元素timeConNeedTest配置不正确!", eint);
			}

			dbConfigVO[num].timeConNeedTest = (timeConNeedTest * 60 * 1000);

			temp = e.getChild("maxCount").getText();
			try {
				tempInt = Integer.parseInt(temp.trim());
			} catch (Exception ex1) {
				throw new CommuException().setMessage(
						"Server.xml中元素maxCount配置不正确!", ex1);
			}
			dbConfigVO[num].maxCount = tempInt;

			temp = e.getChild("minCount").getText();
			try {
				tempInt = Integer.parseInt(temp.trim());
			} catch (Exception ex1) {
				throw new CommuException().setMessage(
						"Server.xml中元素minCount配置不正确!", ex1);
			}
			dbConfigVO[num].minCount = tempInt;

			temp = e.getChild("autoCommit").getText();
			if ((!tempe.getText().equals("true"))
					&& (!tempe.getText().equals("false"))) {
				throw new CommuException().setMessage(
						"Server.xml中元素autoCommit配置不正确!", null);
			}
			if ((temp != null) && (temp.equals("false"))) {
				dbConfigVO[num].autoCommit = false;
			} else {
				dbConfigVO[num].autoCommit = true;
			}

			num++;
		}

		ServerContext.dbvos = dbConfigVO;
	}

	public void getDataSourceName() throws CommuException {
		String[] dataSourceName = (String[]) null;
		Element root = this.doc.getRootElement();
		if (root == null) {
			throw new CommuException().setMessage("没有得到Server.xml配置根元素!", null);
		}
		Element dataSources = root.getChild("dataSources");
		if (dataSources == null) {
			throw new CommuException().setMessage(
					"Server.xml中元素dataSources配置不正确!", null);
		}

		List dataSourceList = dataSources.getChildren();
		if ((dataSourceList == null) || (dataSourceList.size() == 0)) {
			throw new CommuException().setMessage(
					"Server.xml中元素dataSource配置不正确!", null);
		}

		int num = dataSourceList.size();
		dataSourceName = new String[num];
		num = 0;

		Iterator it = dataSourceList.iterator();
		Element tempe = null;
		while (it.hasNext()) {
			Element e = (Element) it.next();
			tempe = e.getChild("dataSourceName");
			if (tempe == null)
				throw new CommuException().setMessage(
						"Server.xml中元素dataSourceName配置不正确!", null);
			dataSourceName[num] = tempe.getText();
			num++;
		}

		ServerContext.dataSourceNames = dataSourceName;
	}

	public void getSaveDataClass() throws CommuException {
		Element root = this.doc.getRootElement();
		if (root == null) {
			throw new CommuException().setMessage("没有得到Server.xml配置根元素!", null);
		}
		Element saveData = root.getChild("saveData");
		if (saveData == null) {
			throw new CommuException().setMessage(
					"Server.xml中元素saveData配置不正确!", null);
		}
		Element clazz = saveData.getChild("class");
		if (clazz == null) {
			throw new CommuException().setMessage(
					"Server.xml中元素saveData.class配置不正确!", null);
		}
		String name = clazz.getText();
		if ((name == null) || (name.trim().equals(""))) {
			throw new CommuException().setMessage(
					"Server.xml中元素saveData.class配置不正确!", null);
		}
		ServerContext.saveDataClass = name.trim();

		Element xmlFileDir = saveData.getChild("xmlFileDir");
		if (xmlFileDir == null) {
			throw new CommuException().setMessage(
					"Server.xml中元素saveData.xmlFileDir配置不正确!", null);
		}
		String xmlFileDirname = xmlFileDir.getText();
		if ((xmlFileDirname == null) || (xmlFileDirname.trim().equals(""))) {
			throw new CommuException().setMessage(
					"Server.xml中元素saveData.xmlFileDir配置不正确!", null);
		}
		ServerContext.xmlFileDir = xmlFileDirname.trim();
		if (xmlFileDirname.indexOf("\\") >= 0) {
			throw new CommuException().setMessage(
					"Server.xml中元素saveData.xmlFileDir配置中不能有\" \\ \"!", null);
		}
		File f = new File(xmlFileDirname);
		if ((f == null) || (!f.exists())) {
			throw new CommuException().setMessage(
					"Server.xml中元素saveData.xmlFileDir配置的路径不存在!", null);
		}

		Element localSaveDate = saveData.getChild("localSaveDate");
		if (localSaveDate == null) {
			throw new CommuException().setMessage(
					"Server.xml中元素saveData.localSaveDate配置不正确!", null);
		}
		String localSaveDatenum = localSaveDate.getText();
		if ((localSaveDatenum == null) || (localSaveDatenum.trim().equals("")))
			throw new CommuException().setMessage(
					"Server.xml中元素saveData.localSaveDate配置不正确!", null);
		try {
			ServerContext.localSaveDate = Integer.parseInt(localSaveDatenum
					.trim());
		} catch (Exception e) {
			throw new CommuException().setMessage(
					"Server.xml中元素saveData.localSaveDate配置不正确,必须为整数!", null);
		}
		if ((ServerContext.localSaveDate < 10)
				|| (ServerContext.localSaveDate > 100)) {
			throw new CommuException().setMessage(
					"Server.xml中元素saveData.localSaveDate配置不正确,值不在合法范围!", null);
		}

		Element saveDataThreadNum = saveData.getChild("saveDataThreadNum");
		if (saveDataThreadNum == null) {
			throw new CommuException().setMessage(
					"Server.xml中元素saveData.saveDataThreadNum配置不正确!", null);
		}
		String saveDataThreadNumInt = saveDataThreadNum.getText();
		if ((saveDataThreadNumInt == null)
				|| (saveDataThreadNumInt.trim().equals("")))
			throw new CommuException().setMessage(
					"Server.xml中元素saveData.saveDataThreadNum配置不正确!", null);
		try {
			ServerContext.saveDataThreadNum = Integer
					.parseInt(saveDataThreadNumInt.trim());
		} catch (Exception e) {
			throw new CommuException()
					.setMessage(
							"Server.xml中元素saveData.saveDataThreadNum配置不正确,必须为整数!",
							null);
		}
		if ((ServerContext.saveDataThreadNum < 2)
				|| (ServerContext.saveDataThreadNum > 100))
			throw new CommuException().setMessage(
					"Server.xml中元素saveData.saveDataThreadNum配置不正确,值不在合法范围!",
					null);
	}
}